import {
	defineStore
} from "pinia";
import {
	marked
} from "marked";
export const useConversationStore = defineStore("conversation", {
	state: () => ({
		// messages: [], // 存储对话消息
		// voiceMessages: [],
		// isAiResponding: false, // 新增
		// message_suggestedList: ["中犇教育科技", "AI硅基教学智能体", "百道云AI无代码开发平台"],
		// // Speaking: false, //数字人是否正在说话
		// currentDevice: "",
		// currentRequestId: null,
		
		
		
		title: "小H",
		subtitle: "高博会数字服务官",
		messages: [], // 存储对话消息
		voiceMessages: [],
		isAiResponding: false, // 新增
		message_suggestedList: ["介绍一下自己", "介绍一下高博会", "展会时间"],
		messageList: [ "展"], 
		// Speaking: false, //数字人是否正在说话
		currentDevice: "",
		currentRequestId: null,
	}),
	actions: {
		// ChangeSpeaking(status) {
		// 	this.Speaking = status
		// },
		ChangeDevice(type) {
			this.currentDevice = type;
		},
		addMessage_suggestList(List) {
			this.message_suggestedList = [];
			this.message_suggestedList = List;
		},
		// 添加用户消息
		addMessage({
			content,
			answer,
			isUser,
			id,
			type
		}) {
			if (id) {
				if (type == "voice") {
					console.log("this.voiceMessages", this.voiceMessages);
					this.voiceMessages = this.voiceMessages.map(item => {
						if (item.id == id) {
							item.answer = answer;
						}
						return item;
					});
				} else {
					console.log("this.messages", this.messages);
					this.messages = this.messages.map(item => {
						if (item.id == id) {
							item.answer = answer;
						}
						return item;
					});
				}
			}
		},

		// 添加AI回复
		async addAiResponse(answer, originalId, type) {
			try {
				this.addMessage({
					content: null,
					answer: await marked(answer),
					isUser: false,
					id: originalId,
					type,
				});
			} catch (error) {
				console.log(error);
			} finally {
				this.isAiResponding = false;
			}
		},
		// 发送消息并获取回复
		sendMessage(content, id, type) {
			const newRequestId = id || Date.now();

			if (this.isAiResponding) {
				// 如果AI已经在响应中，就中断并清空之前未完成的回复
				this.clearPendingAiResponse();
			}
			if (type == "voice") {
				if (this.voiceMessages.length) {
					this.voiceMessages = [
						...this.voiceMessages,
						{
							content,
							answer: null,
							isUser: true,
							id: newRequestId,
							timestamp: new Date().toISOString(),
						},
					];
					this.voiceMessages = [...new Set(this.voiceMessages)];
				} else {
					this.voiceMessages.push({
						content,
						answer: null,
						isUser: true,
						id: newRequestId,
						timestamp: new Date().toISOString(),
					});
				}
			} else {
				if (this.messages.length) {
					this.messages = [
						...this.messages,
						{
							content,
							answer: null,
							isUser: true,
							id: newRequestId,
							timestamp: new Date().toISOString(),
						},
					];
				} else {
					this.messages.push({
						content,
						answer: null,
						isUser: true,
						id: newRequestId,
						timestamp: new Date().toISOString(),
					});
				}
			}
		},
		clearPendingAiResponse() {
			if (type === "voice") {
				this.voiceMessages = this.voiceMessages.filter(
					msg => msg.answer !== null
				);
			} else {
				this.messages = this.messages.filter(
					msg => msg.answer !== null
				);
			}
		},
	},
});